package com.jlsoft.config.oauth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.LinkedList;
import java.util.List;

/**
 * 认证服务器
 * --4中授权模式
 * 1：授权码模式
 * 2：简化模式
 * 3：密码模式
 * 4：客户端模式
 *
 * #授权码模式测试地址：http://localhost:8083/oauth/authorize?response_type=code&client_id=imooc&redirect_uri=http://example.com&scope=all
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired(required = false)
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    @Autowired(required = false)
    private TokenEnhancer jwtTokenEnhancer;


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService);

        //当jwtAccessTokenConverter不为null的时候，才使用。否则，使用securit默认的
        if (null != jwtAccessTokenConverter && null != jwtTokenEnhancer) {
            TokenEnhancerChain chain = new TokenEnhancerChain();

            List<TokenEnhancer> enhancers = new LinkedList<TokenEnhancer>();
            enhancers.add(jwtTokenEnhancer);
            enhancers.add(jwtAccessTokenConverter);

            chain.setTokenEnhancers(enhancers);

            endpoints.tokenEnhancer(chain)
                    .accessTokenConverter(jwtAccessTokenConverter);

            //endpoints.accessTokenConverter(jwtAccessTokenConverter);
        }

    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("imooc").secret("imoocsecret")
                .accessTokenValiditySeconds(7200)//token时效
                .authorizedGrantTypes("refresh_token","authorization_code","password")
                .refreshTokenValiditySeconds(2592000)//refreshToken时效
                .scopes("all","read","write")
        ;
    }
}